/** 
 * @ClassName: DicParamIndexDao 
 * @Description:  绯荤粺閰嶉グ绱㈠紩琛?
 * @author 832
 * @date 2016骞?10鏈?11鏃? 
 *  
 */
package com.fzrskj.cloud.free.dic.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.dic.bean.DicParamIndex;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.List;
import java.util.Map;


public class DicParamIndexDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public DicParamIndexDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}
	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	 /** 查询指定id列表
	 * @param paramType
	 * @param ids
	 * @return
	 */
	public List<DicParamIndex> getSysParamIndexListByIds(int paramType, List<Integer> ids) {
		String sql = "select distinct i from dic_paramindex i,sys_paramItem t where i.paramType=? and t.id in(";
		String sql2=") and i.id=t.paramId";
		String str = "";
		for(int i = 0;i<ids.size();i++){
			str += ids.get(i)+",";
		}
		str=str.substring(0,str.length()-1);
		sql +=str+sql2;
		List<DicParamIndex> sysparamIndexList = db.queryForBeans(sql, DicParamIndex.class, new Object[]{ids,paramType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sysparamIndexList;
	}
	/**
	 * 获取数据字典
	 * @param paramType
	 * @return
	 */
	public List<DicParamIndex> getSysParamIndexList(int paramType) {
		String sql = "select * from dic_paramindex where paramType=?";
		List<DicParamIndex> list = db.queryForBeans(sql, DicParamIndex.class,new Object[]{paramType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return list;
	}
	/**
	 * 通过英文名获取字段值
	 * @param paramEName
	 * @param pramType
	 * @return
	 */
	public DicParamIndex getSysParamIndexByEName(String paramEName,int pramType) {
		String sql = "select * from dic_paramindex where paramEName=? and paramType=?";
		DicParamIndex sysParamIndex = db.queryForBean(sql,DicParamIndex.class,new Object[]{paramEName,pramType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return sysParamIndex;
	}
	/**
	 * 更新系统配置索引表
	 * @param dicParamIndex
	 * @return
	 */
	public int updateParamIndex(DicParamIndex dicParamIndex){
		String sql = "update dic_paramindex set paramCName=? where id=? and paramEName=?";
		int ret = db.executeSql(sql,new Object[]{dicParamIndex.getParamCName(),dicParamIndex.getId(),dicParamIndex.getParamEName()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	/**
	 * 添加系统配置索引表
	 * @param obj
	 * @return
	 */
	public int addSysParamindex(DicParamIndex obj){
		String sql = "insert into dic_paramindex(paramCName,paramEName,paramType)values(?,?,?)";
		Object[] prm={obj.getParamCName(),obj.getParamEName(),obj.getParamType()};
		int ret = db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	/**
	 * 根据id与paramEName删除对应的数据字典表
	 * @param id
	 * @param paramEName
	 * @return
	 */
	public int delSysParamindex(int id,String paramEName){
		String sql="delete from dic_paramindex where id=? and paramEName=?";
		int ret = db.executeSql(sql,new Object[]{id,paramEName});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database by map
	public int addSysParamindex(Map<String, Object> values){
		int ret = db.insertRecord("dic_paramindex",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}



	//to delete table record by cdt
	public int delSysParamindex(Map<String, Object> cdt){
		int ret = db.deleteRecord("dic_paramindex",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modSysParamindex(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("dic_paramindex", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query sys_paramindex item for detail
	public DicParamIndex querySysParamIndex(int id){
		String sql="select * from dic_paramindex where id=?";
		DicParamIndex ret = db.queryForBean(sql,DicParamIndex.class,new Object[]{id});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query SysParamindex record count
	public int querySysParamindex(Map<String, Object> cdt){
		int ret = db.queryRecordCount("dic_paramindex", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to query SysParamindex for record list with condition,if page or pagesize less than 0, means without page
	public List<DicParamIndex> querySysParamindexList(Map<String, Object> cdt, int page, int pagesize){
		List<DicParamIndex> ret = db.queryForBeans("dic_paramindex", "*", " id desc ", DicParamIndex.class, cdt, page, pagesize);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}